package com.inmaytide.website.service.system.impl;

import com.inmaytide.website.domain.entities.system.SysUser;
import com.inmaytide.website.service.system.SysUserService;
import com.inmaytide.website.utils.pagination.Pagination;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private Mapper<SysUser> mapper;

    @Override
    public List<SysUser> findList(SysUser condition) {
        return mapper.select(condition);
    }

    @Override
    public SysUser get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysUser> findList() {
        return mapper.selectAll();
    }

    @Override
    @Transactional
    public SysUser insert(SysUser inst) {
        mapper.insertSelective(inst);
        return inst;
    }

    @Override
    public SysUser get(String loginName) {
        SysUser condition = new SysUser();
        condition.setLoginName(loginName);
        List<SysUser> list = findList(condition);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

    @Override
    @Transactional
    public void update(SysUser inst) {
        mapper.updateByPrimaryKey(inst);
    }

    @Override
    public Pagination<SysUser> findList(Pagination<SysUser> pagination) {
        List<SysUser> list = mapper.selectByRowBounds((SysUser) pagination.getConditions(), pagination.getRowBounds());
        pagination.setResult(list);
        return pagination;
    }

}
